package com.lge.qcontact.threads;

import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.lge.qcontact.utils.MUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;



/**
 * 
 * @author project
 * server DB로부터 account 정보를 검색하는 thread
 */
public class FindAccountThread extends Thread {
	private String tempName = null;
	private String tempEmail = null;
	private String tempId = null;
	private String tempPhone = null;
	private String tempPwd = null;
	
	private String editName = null;
	private String editEmail = null;
	private String editId = null;
	private String editPhone = null;
	private int selectedMode = 0; 		// 0: id 찾기, 1: pwd 찾기
	
	private String resultId = null; 	// ID 검색 결과
	private String resultPwd = null; 	// 비밀번호 검색 결과

	
	/* 아이디 찾기 - 서버 DB 의 결과 받아옴 */
	Handler mHandler;
	public FindAccountThread(Handler handler, int mode, String name, String email, String id, String phone) {
	
		mHandler = handler;
		
		if(name != null){
			editName = name.trim();
		}
			
		if(email != null){	
			editEmail = email.trim();
		}
		
		if(id != null){		
			editId = id.trim();
		}
		
		if(phone != null){	
			editPhone = phone.trim();
		}
		
		selectedMode = mode;
	}
		
	public void run(){
		MUtils.log("FIND_ACCOUNT", "[Thread Run] Start FindAccoutnThread");
		
		URL url;
		XmlPullParserFactory factory;
		XmlPullParser parser;
		
		int eventType;
		
		String tag = null;
		 
		try {
			url = new URL("http://192.168.0.205:8080/com.lg.project.myexample.cmd.RetrieveMemberUserJoinListCmd.vcc");
	
			factory = XmlPullParserFactory.newInstance();
			parser = factory.newPullParser();
			parser.setInput(url.openStream(), "utf-8");
			eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) { // 테이블 읽기
				tag = parser.getName();
				switch(eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if(selectedMode == 0){ // ID 찾기 모드 - 이름, email 정보 이용 
						if (tag.equals("uid")) {
							tempId  = parser.nextText();
						}else if(tag.equals("uname")){
							tempName = parser.nextText();
						}else if(tag.equals("email")){
							tempEmail = parser.nextText();
						}
				
						if(editName.equals(tempName) && editEmail.equals(tempEmail)){
							MUtils.log("FIND_ACCOUNT", "   Success finiding ID : " + tempId);
							resultId = tempId;
							eventType = XmlPullParser.END_DOCUMENT;
						}
					}else if(selectedMode == 1){ // 비밀번호 찾기 모드 - ID, 핸드폰번호 정보 이용
						if (tag.equals("uid")) {
							tempId  = parser.nextText();
						}else if(tag.equals("upw")){
							tempPwd = parser.nextText();
						}else if(tag.equals("mobile")){
							tempPhone = parser.nextText();
						}		
						
						if(editId.equals(tempId) && editPhone.equals(tempPhone)){
							MUtils.log("FIND_ACCOUNT", "   Success finiding password...");
							resultPwd = tempPwd;
							eventType = XmlPullParser.END_DOCUMENT;
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if(tag.startsWith("data"))
						tempName = tempEmail = tempId = tempPhone = tempPwd = null;
					
					break;
				}
				eventType = parser.next();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} // 테이블 검색 완료
	        
		MUtils.log("FIND_ACCOUNT", "   Sending data to handler...");
		
		/* 핸들러에 ID 검색 결과 여부 전송 */
        Message msg = Message.obtain();
        Bundle data = new Bundle();
        switch(selectedMode){
        case 0:
        	data.putString("ID", resultId);
        	break;
        case 1:
        	data.putString("PWD", resultPwd);
        	break;
       	default:
        	break;
        }
        msg.setData(data);
        mHandler.sendMessage(msg);

        MUtils.log("FIND_ACCOUNT", "[Thread Run] Finish FindAccoutnThread");
		
	}
}
